import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

// 用户端路由
const UserLayout = () => import('../views/user/Layout.vue')
const UserLogin = () => import('../views/user/Login.vue')
const UserRegister = () => import('../views/user/Register.vue')
const UserIndex = () => import('../views/user/Index.vue')
const UserTasks = () => import('../views/user/Tasks.vue')
const UserPublishedTasks = () => import('../views/user/PublishedTasks.vue')
const UserAcceptedTasks = () => import('../views/user/AcceptedTasks.vue')
const UserNewTask = () => import('../views/user/NewTask.vue')
const UserProfile = () => import('../views/user/Profile.vue')
const UserSettings = () => import('../views/user/Settings.vue')

// 管理端路由
const AdminLayout = () => import('../views/admin/Layout.vue')
const AdminLogin = () => import('../views/admin/Login.vue')
const AdminIndex = () => import('../views/admin/Index.vue')
const AdminUsers = () => import('../views/admin/Users.vue')
const AdminTasks = () => import('../views/admin/Tasks.vue')
const AdminSchools = () => import('../views/admin/Schools.vue')
const AdminSettings = () => import('../views/admin/Settings.vue')
const AdminIncomePending = () => import('../views/admin/IncomePending.vue')
const AdminIncomeSettled = () => import('../views/admin/IncomeSettled.vue')
const AdminIncomeTotal = () => import('../views/admin/IncomeTotal.vue')
const AdminTest = () => import('../views/admin/Test.vue')

const routes = [
  {
    path: '/',
    redirect: '/user'
  },
  {
    path: '/user',
    component: UserLayout,
    children: [
      {
        path: '',
        redirect: '/user/index'
      },
      {
        path: 'login',
        name: 'UserLogin',
        component: UserLogin
      },
      {
        path: 'register',
        name: 'UserRegister',
        component: UserRegister
      },
      {
        path: 'index',
        name: 'UserIndex',
        component: UserIndex,
        meta: { requiresAuth: true }
      },
      {
        path: 'tasks',
        name: 'UserTasks',
        component: UserTasks,
        meta: { requiresAuth: true }
      },
      {
        path: 'published-tasks',
        name: 'UserPublishedTasks',
        component: UserPublishedTasks,
        meta: { requiresAuth: true }
      },
      {
        path: 'accepted-tasks',
        name: 'UserAcceptedTasks',
        component: UserAcceptedTasks,
        meta: { requiresAuth: true }
      },
      {
        path: 'new-task',
        name: 'UserNewTask',
        component: UserNewTask,
        meta: { requiresAuth: true }
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: UserProfile,
        meta: { requiresAuth: true }
      },
      {
        path: 'settings',
        name: 'UserSettings',
        component: UserSettings,
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/admin',
    component: AdminLayout,
    children: [
      {
        path: '',
        redirect: '/admin/index'
      },
      {
        path: 'login',
        name: 'AdminLogin',
        component: AdminLogin
      },
      {
        path: 'index',
        name: 'AdminIndex',
        component: AdminIndex,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: AdminUsers,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'tasks',
        name: 'AdminTasks',
        component: AdminTasks,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'schools',
        name: 'AdminSchools',
        component: AdminSchools,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'settings',
        name: 'AdminSettings',
        component: AdminSettings,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'income-pending',
        name: 'AdminIncomePending',
        component: AdminIncomePending,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'income-settled',
        name: 'AdminIncomeSettled',
        component: AdminIncomeSettled,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'income-total',
        name: 'AdminIncomeTotal',
        component: AdminIncomeTotal,
        meta: { requiresAuth: true, requiresAdmin: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth) {
    if (!userStore.isLoggedIn) {
      next({ name: to.path.startsWith('/admin') ? 'AdminLogin' : 'UserLogin' })
      return
    }
    
    if (to.meta.requiresAdmin && !userStore.isAdmin) {
      next({ name: 'UserIndex' })
      return
    }
  }
  
  next()
})

export default router
